<template>
	<view class="content">
		<view :class="show ? 'fade-in' : 'fade-out'">
			<view class="list-box"  v-if="addressList.length != 0">
				<view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
					<view class="wrapper">
						<view class="u-box tn-flex tn-flex-col-center">
							<text class="name text-bold">{{item.name}}</text>
							<text class="name mobile text-bold">{{item.mobile}}</text>
							<image src="/static/index/moren.png" v-if="item.status =='默认'" class="morenIcon" mode="">
							</image>
						</view>
						<text class="address"> {{item.address}} {{item.housenumber}}</text>
					</view>
					<image src="/static/index/citydel.png" @click.stop="del(item.id)" class="delImg" mode=""></image>
				</view>
			</view>
			<view class="kong" v-else>
				<image src="https://peiwanblogs.huolieniaowangluo.com/wx-image/wfw/kong.png" mode=""></image>
				<text>暂无地址</text>
			</view>
			<view class="add-btn" @click="addAddress('add')">
				新建地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sid: 811,
				source: 0,
				addressList: [],
				type: '',
				show: false
			}
		},
		onLoad(option) {
			let that = this
			if (option.source) {
				this.source = option.source;
			}
			if (option.sid) {
				this.sid = option.sid;
			}
			if (option.type) {
				this.type = option.type
			}
			this.init();
			uni.$off('addaddresok')
			uni.$on('addaddresok', function(data) {
				that.init();
			})
		},
		methods: {
			async init() {
				let that = this;
				let result = await that.$request({
					loading: 0,
					method: 'post',
					url: '/api/shop/my_address',
					data: {
						//goods_id:this.id
					}
				});
				if (result.data.code == 200) {
					this.addressList = result.data.data;
					this.show = true


				}
			},


			//选择地址
			checkAddress(item, fanwei) {
				if (this.type == 1) {
					uni.$emit('addxuanze', item)
					uni.setStorage({
						key: 'selectAddress',
						data: item,
						success() {
							uni.navigateBack();
						}
					})
				}



			},
			addAddress(type, item = []) {
				uni.navigateTo({
					url: `/shopPages/address_add?type=${type}&data=${JSON.stringify(item)}`
				})
			},

			async del(id) {
				let result = await this.$request({
					method: 'post',
					url: '/api/User/addressDel',
					data: {
						id
					}
				})
				if (result.data.code == 200) {
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					})
					this.init();
				}
			},
			//添加或修改成功之后回调
			refreshList(data, type) {
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);

				console.log(data, type);
			}
		}
	}
</script>

<style lang='scss' scoped>
	.content {
		position: relative;
		padding: 30rpx 30rpx 200rpx 30rpx;

		.kong {
			height: 50vh;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				width: 520rpx;
				height: 301rpx;
			}

			text {
				font-size: 32rpx;
				font-weight: 500;
				color: #CCCCCC;
				margin-top: 32rpx;
			}
		}

	}

	.list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 146rpx;
		padding: 0 32rpx;
		margin-bottom: 24rpx;
		background: #ffffff;
		border-radius: 12rpx;
	}

	.chaoguo {
		color: #ff0000;
	}

	.wrapper_fanwei {
		color: #0081FF;
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address {
		font-size: 26rpx;
		font-weight: 400;
		color: #333333;
		margin-top: 16rpx;
	}

	.delImg {
		width: 38rpx;
		height: 38rpx;
	}

	.u-box {
		font-size: 28upx;
		color: #333333;

		.name {
			margin-right: 20rpx;
		}

		.mobile {
			margin-right: 8rpx;
		}

		.morenIcon {
			width: 68rpx;
			height: 40rpx;
		}
	}

	.icon-edit {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: #333333;
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		border-radius: 10px;
		overflow: hidden;
		bottom: 40rpx;
		left: 30rpx;
		right: 30rpx;
		height: 98rpx;
		background: linear-gradient(270deg, #DB89F5 0%, #834BF4 100%);
		border-radius: 20rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
	}
</style>
